﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.DXErrorProvider;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmRegionalSetup : XtraForm
    {
        public event AddToMainEventHandler AddToMain;
        public FrmRegionalSetup()
        {
            this.table = "MXHLY_SYS_REGIONAL";
            this.fid = "RGN_ID";
            this.InitializeComponent();
            this.cbeRgnLayer.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
        }
        public FrmRegionalSetup(string rid)
        {
            this.table = "MXHLY_SYS_REGIONAL";
            this.fid = "RGN_ID";
            this.InitializeComponent();
            this.cbeRgnLayer.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.BindRgnLayer();
            this.RGN_ID.Properties.ReadOnly = false;
            if (rid != null)
            {
                this.RGN_ID.Text = rid;
                string commandtext = "select * from " + this.table + " where " + this.fid + " =:dictid";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("dictid", rid) });
                DataTable dt = new DataTable();
                if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt.Rows.Count == 1))
                {
                    this.RGN_ID.Properties.ReadOnly = true;
                    this.RGN_ACTIVE.FieldValue = dt.Rows[0]["RGN_ACTIVE"].ToString();
                    this.RGN_CODE.FieldValue = dt.Rows[0]["RGN_CODE"].ToString();
                    this.RGN_FULLNAME.FieldValue = dt.Rows[0]["RGN_FULLNAME"].ToString();
                    this.RGN_IS_LEAF.FieldValue = dt.Rows[0]["RGN_IS_LEAF"].ToString();
                    this.cbeRgnLayer.EditValue = this.GetTextByValue(typeof(RgnLayer), dt.Rows[0]["RGN_LAYER"].ToString());
                    this.RGN_NAME.FieldValue = dt.Rows[0]["RGN_NAME"].ToString();
                    this.RGN_PARENT_ID.FieldValue = dt.Rows[0]["RGN_PARENT_ID"].ToString();
                }
            }
        }

        public FrmRegionalSetup(string parentId, string code, string layer)
        {
            this.table = "MXHLY_SYS_REGIONAL";
            this.fid = "RGN_ID";
            this.InitializeComponent();
            this.cbeRgnLayer.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.BindRgnLayer();
            this.RGN_PARENT_ID.FieldValue = parentId;
            this.RGN_CODE.FieldValue = code;
            this.cbeRgnLayer.EditValue = this.GetTextByValue(typeof(RgnLayer), layer);
        }

        private void BindRgnLayer()
        {
            this.cbeRgnLayer.Properties.Items.Add(new RgnLayer("1", "省"));
            this.cbeRgnLayer.Properties.Items.Add(new RgnLayer("2", "市"));
            this.cbeRgnLayer.Properties.Items.Add(new RgnLayer("3", "区(县)"));
            this.cbeRgnLayer.Properties.Items.Add(new RgnLayer("4", "街道(镇)"));
            this.cbeRgnLayer.Properties.Items.Add(new RgnLayer("5", "社区(村)"));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Yes;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dxValidationProvider1.Validate())
            {
                DataParamCol parm = new DataParamCol();
                parm.Add(new InputDataParam("RGN_PARENT_ID", this.RGN_PARENT_ID.FieldValue));
                string commandtext = "select * from MXHLY_SYS_REGIONAL where RGN_ID=:RGN_PARENT_ID";
                DataTable dt = new DataTable();
                if ((SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt != null)) && ((dt.Rows.Count > 0) && (dt.Rows[0]["RGN_IS_LEAF"].ToString() == "Y")))
                {
                    MessageBox.Show("该节点为末级节点，不能新增子节点");
                }
                else
                {
                    DataParamCol parms = new DataParamCol();
                    string str2 = "";
                    if (SystemInfo.DbData.IsExist(this.table, this.fid, this.RGN_ID.Text.Trim()))
                    {
                        string sql = string.Format("Select * From MXHLY_SYS_REGIONAL where RGN_CODE='{0}' and RGN_ID!={1}", this.RGN_CODE.FieldValue.ToString(), this.RGN_ID.Text.Trim());
                        if (SystemInfo.DbData.GetCount(sql, null) >= 1)
                        {
                            MessageBox.Show("该行政机构代码已经存在，请重新输入");
                            return;
                        }
                        parms.Add(new InputDataParam("RGN_ID", this.RGN_ID.Text.Trim()));
                        parms.Add(new InputDataParam("RGN_FULLNAME", this.RGN_FULLNAME.FieldValue));
                        parms.Add(new InputDataParam("RGN_NAME", this.RGN_NAME.FieldValue));
                        parms.Add(new InputDataParam("RGN_CODE", this.RGN_CODE.FieldValue));
                        parms.Add(new InputDataParam("RGN_LAYER", this.GetValueByText(typeof(RgnLayer), this.cbeRgnLayer.SelectedItem.ToString())));
                        parms.Add(new InputDataParam("RGN_PARENT_ID", this.RGN_PARENT_ID.FieldValue));
                        parms.Add(new InputDataParam("RGN_IS_LEAF", this.RGN_IS_LEAF.FieldValue));
                        parms.Add(new InputDataParam("RGN_ACTIVE", this.RGN_ACTIVE.FieldValue));
                        parms.Add(new InputDataParam("RGN_CREATE_ID", SystemInfo.OperId));
                        parms.Add(new InputDataParam("CREATE_ORG_CODE", SystemInfo.OrgHeadCode));
                        str2 = string.Format("Update MXHLY_SYS_REGIONAL Set RGN_FULLNAME=:RGN_FULLNAME, RGN_NAME=:RGN_NAME,RGN_CODE=:RGN_CODE,RGN_LAYER=:RGN_LAYER, RGN_PARENT_ID=:RGN_PARENT_ID, RGN_IS_LEAF=:RGN_IS_LEAF,RGN_ACTIVE=:RGN_ACTIVE, RGN_CREATE_ID=:RGN_CREATE_ID,RGN_CREATE_DATE=SYSDATE,CREATE_ORG_CODE=:CREATE_ORG_CODE Where RGN_ID=:RGN_ID", new object[0]);
                    }
                    else
                    {
                        string str4 = string.Format("Select * From MXHLY_SYS_REGIONAL where RGN_CODE='{0}'", this.RGN_CODE.FieldValue.ToString());
                        if (SystemInfo.DbData.GetCount(str4, null) >= 1)
                        {
                            MessageBox.Show("该行政机构代码已经存在，请重新输入");
                            return;
                        }
                        str2 = "select MAX(" + this.fid + ") from " + this.table;
                        int data = 1;
                        SystemInfo.DbData.ExecuteScalar<int>(str2, ref data);
                        this.RGN_ID.Text = (++data).ToString();
                        parms.Add(new InputDataParam("RGN_ID", this.RGN_ID.Text.Trim()));
                        parms.Add(new InputDataParam("RGN_FULLNAME", this.RGN_FULLNAME.FieldValue));
                        parms.Add(new InputDataParam("RGN_NAME", this.RGN_NAME.FieldValue));
                        parms.Add(new InputDataParam("RGN_CODE", this.RGN_CODE.FieldValue));
                        parms.Add(new InputDataParam("RGN_LAYER", this.GetValueByText(typeof(RgnLayer), this.cbeRgnLayer.SelectedItem.ToString())));
                        parms.Add(new InputDataParam("RGN_PARENT_ID", this.RGN_PARENT_ID.FieldValue.ToString()));
                        parms.Add(new InputDataParam("RGN_IS_LEAF", this.RGN_IS_LEAF.FieldValue));
                        parms.Add(new InputDataParam("RGN_ACTIVE", this.RGN_ACTIVE.FieldValue));
                        parms.Add(new InputDataParam("RGN_CREATE_ID", SystemInfo.OperId));
                        parms.Add(new InputDataParam("CREATE_ORG_CODE", SystemInfo.OrgHeadCode));
                        str2 = string.Format("insert into MXHLY_SYS_REGIONAL(RGN_ID, RGN_FULLNAME, RGN_NAME, RGN_CODE, RGN_LAYER, RGN_PARENT_ID, RGN_IS_LEAF, RGN_ACTIVE, RGN_CREATE_ID,RGN_CREATE_DATE,CREATE_ORG_CODE) values(:RGN_ID, :RGN_FULLNAME, :RGN_NAME, :RGN_CODE, :RGN_LAYER, :RGN_PARENT_ID, :RGN_IS_LEAF, :RGN_ACTIVE, :RGN_CREATE_ID, SYSDATE, :CREATE_ORG_CODE)", new object[0]);
                    }
                    if (SystemInfo.DbData.IsOpen)
                    {
                        if (SystemInfo.DbData.ExecuteSql(str2, parms) == 1)
                        {
                            this.AddToMain(sender, e);
                            base.Close();
                        }
                        else
                        {
                            XtraMessageBox.Show(SystemInfo.DbData.ErrorMessage, "数据库访问错误");
                        }
                    }
                }
            }
        }
        private void FrmRegionalSetup_Load(object sender, EventArgs e)
        {
            ConditionValidationRule rule = new ConditionValidationRule
            {
                ConditionOperator = ConditionOperator.IsNotBlank,
                ErrorText = "该值不能为空！"
            };
            this.dxValidationProvider1.SetValidationRule(this.RGN_CODE, rule);
            this.dxValidationProvider1.SetValidationRule(this.RGN_NAME, rule);
            this.dxValidationProvider1.SetValidationRule(this.RGN_FULLNAME, rule);
        }

        private string GetTextByValue(Type type, string RgnTypeValue)
        {
            if (type == typeof(RgnLayer))
            {
                foreach (RgnLayer layer in this.cbeRgnLayer.Properties.Items)
                {
                    if (layer.RgnLayerNum == RgnTypeValue)
                    {
                        return layer.RgnLayerName;
                    }
                }
            }
            return "未知";
        }

        private string GetValueByText(Type type, string RgnTypeText)
        {
            if (type == typeof(RgnLayer))
            {
                foreach (RgnLayer layer in this.cbeRgnLayer.Properties.Items)
                {
                    if (layer.RgnLayerName == RgnTypeText)
                    {
                        return layer.RgnLayerNum;
                    }
                }
            }
            return "未知";
        }
        // Nested Types
        public delegate void AddToMainEventHandler(object sender, EventArgs e);

        private class RgnLayer
        {
            // Fields
            private string _RgnLayerName;
            private string _RgnLayerNum;

            // Methods
            public RgnLayer(string RgnLayerNum, string RgnLayerName)
            {
                this._RgnLayerNum = RgnLayerNum;
                this._RgnLayerName = RgnLayerName;
            }

            public override string ToString()
            {
                return this._RgnLayerName;
            }

            // Properties
            public string RgnLayerName
            {
                get
                {
                    return this._RgnLayerName;
                }
                set
                {
                    this._RgnLayerName = value;
                }
            }

            public string RgnLayerNum
            {
                get
                {
                    return this._RgnLayerNum;
                }
                set
                {
                    this._RgnLayerNum = value;
                }
            }
        }
    }
}
